package com.briup.pts.web.controller;

import com.briup.pts.config.response.Result;
import com.briup.pts.domain.bean.IndexRecord;
import com.briup.pts.service.IndexRecordService;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/indexRecord")
@RequiredArgsConstructor
public class IndexRecordController {

    private final IndexRecordService service;

    @GetMapping("/query")
    public Result<PageInfo<IndexRecord>> pageQuery(
            @Min(value = 1,message = "当前页面至少要不小于1") Integer pageNum,
            @Min(value = 1,message = "页面大小至少要不小于1") Integer pageSize,
            Integer startWeight,Integer endWeight){
        // 调用service层方法，进行分页查询
        PageInfo<IndexRecord> pageInfo = service
                .pageQuery(pageNum, pageSize, startWeight, endWeight);
        return Result.success(pageInfo);
    }

    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@Valid @RequestBody IndexRecord indexRecord){

        service.saveOrUpdate(indexRecord);
        return Result.success();
    }

    @DeleteMapping("/deleteById/{irdId}")
    public Result<?> deleteById(@PathVariable String irdId) {
        service.deleteBatch(List.of(irdId));
        return Result.success();
    }

    @DeleteMapping("/deleteByIdAll")
    public Result<?> deleteByIdAll(@RequestBody List<String> ids) {
        service.deleteBatch(ids);
        return Result.success();
    }
}
